package cn.kduck.module.organization.web;

import cn.kduck.core.service.Page;
import cn.kduck.core.service.ValueMapList;
import cn.kduck.core.utils.TreeNodeUtils;
import cn.kduck.core.utils.TreeNodeUtils.Node;
import cn.kduck.core.web.annotation.ModelOperate;
import cn.kduck.core.web.annotation.ModelResource;
import cn.kduck.core.web.json.JsonObject;
import cn.kduck.core.web.json.JsonPageObject;
import cn.kduck.module.organization.service.Organization;
import cn.kduck.module.organization.service.OrganizationService;
import cn.kduck.module.user.service.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/organization")
@ModelResource("组织机构管理")
public class OrganizationController {

    @Autowired
    private OrganizationService orgService;

    /**
     * 添加组织机构
     *
     * @param organization
     * @return
     */
    @PostMapping("/add")
    @ModelOperate(name="添加组织机构")
    public JsonObject addOrganization(Organization organization) {
        orgService.addOrganization(organization);
        return JsonObject.SUCCESS;
    }

    /**
     * 更新组织机构
     *
     * @param organization
     * @return
     */
    @PutMapping("/update")
    @ModelOperate(name="更新组织机构")
    public JsonObject updateOrganization(Organization organization) {
        orgService.updateOrganization(organization);
        return JsonObject.SUCCESS;
    }

    /**
     * 删除组织机构
     * @return
     */
    @DeleteMapping("/delete")
    @ModelOperate(name="删除组织机构")
    public JsonObject deleteOrganization(@RequestParam("ids") String[] ids) {
        orgService.deleteOrganization(ids);
        return JsonObject.SUCCESS;
    }

    /**
     * 根据组织机构主键查询组织机构信息
     *
     * @param orgId
     * @return
     */
    @GetMapping("/get")
    @ModelOperate(name="根据主键查询组织机构")
    public JsonObject getOrganization(@RequestParam("orgId") String orgId) {
        Organization organization = orgService.getOrganization(orgId);
        return new JsonObject(organization);
    }

    /**
     * 分页查询组织机构列表
     * @return
     */
    @GetMapping("/list")
    @ModelOperate(name="分页查询组织机构列表")
    public JsonObject listOrganization(@RequestParam("parentId") String parentId, @RequestParam Map<String, Object> paramMap, Page page) {
        List<Organization> organizations = orgService.listOrganizationByParentId(parentId,paramMap,page);
        return new JsonObject(organizations);
    }

    @GetMapping("/tree")
    @ModelOperate(name="查询机构详情(树结构封装)")
    public JsonObject listOrganizationTree(){
        List<Organization> valueMaps = orgService.listAllOrganization(true);
        List<Node<Organization>> nodes = TreeNodeUtils.formatTreeNode(valueMaps, Organization::getOrgId, Organization::getOrgName, Organization::getParentId);
        return new JsonObject(nodes);
    }



    @PostMapping("/user/add")
    @ModelOperate(name="新增机构用户")
    public JsonObject addOrgUser(@RequestParam("orgId") String orgId,@RequestParam("userIds") String[] userIds){
        orgService.addOrgUser(orgId,userIds);
        return JsonObject.SUCCESS;
    }

    @DeleteMapping("/user/delete")
    @ModelOperate(name="删除机构用户")
    public JsonObject deleteOrgUser(@RequestParam("ids") String[] orgUserIds){
        orgService.deleteOrgUser(orgUserIds);
        return JsonObject.SUCCESS;
    }

    @GetMapping("/user/list")
    @ModelOperate(name="根据机构ID查询机构用户")
    public JsonObject listOrgUser(@RequestParam("orgId") String orgId, Page page){
        ValueMapList valueMaps = orgService.listOrgUser(orgId,page);
        return new JsonPageObject(page,valueMaps);
    }

    @GetMapping("/user/list/forSelect")
    @ModelOperate(name="根据机构ID查询可选用户")
    public JsonObject listOrgUserForSelect(@RequestParam("orgId") String orgId, Page page){
        List<User> organizations = orgService.listUserForOrgUser(orgId, page);
        return new JsonPageObject(page,organizations);
    }

}
